package com.shuqi.model.parse.parser;

import java.util.List;

import a.a.a.a.a.e;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.shuqi.common.utils.Log4an;
import com.shuqi.interfaces.OnAfterLoadPage;
import com.shuqi.model.bean.BookContentInfo;
import com.shuqi.model.bean.ChapterInfo;
import com.shuqi.model.manager.DownLoadBookManager;
import com.shuqi.model.manager.DownLoadBookManager.OneChapterCallback;
import com.shuqi.model.parse.BookContentParserBase;

/**
 * 收费版书籍数据解析
 * 
 * @remark 向底层索要收费书籍数据
 * @author Jianda.Yang
 * 
 * @Date 2014-2-10 上午10:46:15
 */
public class BookContentPayBookParser extends BookContentParserBase {
    /** 当前章缓存 */
    private BookContentInfo currInfo;
    private int fileLen = 0;

    @Override
    public BookContentInfo getCurInfos(Context context, final BookContentInfo params,
            boolean isNext, final OnOneChpaterLoadDone callBack) {

        currInfo = params;

        Log4an.d("yjd", "[索要底层数据]bid=" + params.getBookId() + ",cid=" + params.getCurChapterCid()
                + ",bookname=" + params.getBookname() + "\nuid=" + params.getUid() + ",isNeedBuy="
                + params.isNeedBuy() + ",sourceID=" + params.getSourceId());

        Log4an.d("yjd", "callBack is null=" + (callBack == null));
        ChapterInfo chapterInfo =
                DownLoadBookManager.getCurChapterInfo(context, params.getSourceId(),
                        params.getBookId(), params.getCurChapterCid(), params.isNeedBuy(),
                        params.getUid(), callBack == null ? null : new OneChapterCallback() {

                            @Override
                            public void onOneChapter(ChapterInfo chapterInfo) {
                                Log4an.d("yjd", "【第一章】chapterInfo is null=" + (chapterInfo == null));
                                if (chapterInfo != null) {
                                    setCurrInfo(chapterInfo, params);
                                    if (callBack != null) {
                                        callBack.oneChapterLoadDone(false);
                                    }
                                }else {
                                    if (callBack != null) {
                                        callBack.oneChapterLoadDone(true);
                                    }  
                                }
                            }

                        });
        Log4an.d("yjd", "【返回后的数据】chapterInfo is null=" + (chapterInfo == null));
        setCurrInfo(chapterInfo, params);

        return currInfo;
    }

    private void setCurrInfo(ChapterInfo chapterInfo, BookContentInfo params) {
        if (chapterInfo != null) {

            Log4an.d("yjd", "【setCurrInfo】curTpye=" + chapterInfo.getCurChapterType());
            currInfo.setCurChapterName(chapterInfo.getCurChapterName());
            currInfo.setCurChapterType(chapterInfo.getCurChapterType());
            currInfo.setErrorMessage(chapterInfo.getMsg());
            if (chapterInfo.getCurChapterInfo() != null) {
                byte[] infobytes = chapterInfo.getCurChapterInfo().getBytes();
                fileLen = infobytes.length;
                currInfo.setTotalLength(fileLen);
                currInfo.setCurChapterBytes(infobytes);
            } else {
                currInfo.setCurChapterBytes(null);
            }

            currInfo.setEncoding("UTF-8");
            currInfo.setBookId(chapterInfo.getBookId());
            currInfo.setUid(chapterInfo.getUid());

            currInfo.setCurChapterCid(isNull(chapterInfo.getCurChapterCid()) ? params
                    .getCurChapterCid() : chapterInfo.getCurChapterCid());

            currInfo.setNextChapterCid(chapterInfo.getNextChapterCid());
            currInfo.setPreChapterCid(chapterInfo.getPreChapterCid());

            currInfo.setCurChapterPayMode(chapterInfo.getCurChapterPayMode());
            currInfo.setNextChapterPayMode(chapterInfo.getNextChapterPayMode());
            currInfo.setPreChapterPayMode(chapterInfo.getPreChapterPayMode());

            currInfo.setCurChapterPrice(chapterInfo.getCurChapterPrice());
            currInfo.setNextChapterPrice(chapterInfo.getNextChapterPrice());
            currInfo.setPreChapterPrice(chapterInfo.getPreChapterPrice());

            currInfo.setCurChapterName(chapterInfo.getCurChapterName());
            currInfo.setNextChapterName(chapterInfo.getNextChapterName());
            currInfo.setPreChapterName(chapterInfo.getPreChapterName());

        }
    }

    private boolean isNull(String str) {
        return TextUtils.isEmpty(str);
    }

    @Override
    public BookContentInfo getCurrInfo(Activity context) {
        return null;
    }

    @Override
    public void loadPrevInfo(Activity context, final OnAfterLoadPage onAfterLoadPage,
            BookContentInfo params) {
        synchronized (BookContentPayBookParser.this) {
            final BookContentInfo info = getCurInfos(context, params, false);
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    onAfterLoadPage.runOnUiThread(info);
                }
            });
        }
    }

    @Override
    public void loadNextInfo(Activity context, final OnAfterLoadPage onAfterLoadPage,
            BookContentInfo params) {
        synchronized (BookContentPayBookParser.this) {
            final BookContentInfo info = getCurInfos(context, params, true);
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    onAfterLoadPage.runOnUiThread(info);
                }
            });
        }
    }

    @Override
    public boolean hasPreChapter(int index) {
        Log4an.i("draw", "【收费上章缓存】currInfo is null=" + (currInfo == null));
        if (currInfo == null) {
            return false;
        } else {
            Log4an.i(
                    "draw",
                    "【收费上章缓存】currInfo.getPreChapterCid() is null="
                            + (currInfo.getPreChapterCid() == null ? true : currInfo
                                    .getPreChapterCid()));
            if (!TextUtils.isEmpty(currInfo.getPreChapterCid())) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public boolean hasNextChapter(int index) {
        Log4an.i("draw", "【收费下章缓存】currInfo is null=" + (currInfo == null));
        if (currInfo == null) {
            return false;
        } else {
            Log4an.i(
                    "draw",
                    "【收费下章缓存】currInfo.getPreChapterCid() is null="
                            + (currInfo.getNextChapterCid() == null ? true : currInfo
                                    .getNextChapterCid()));
            if (!TextUtils.isEmpty(currInfo.getNextChapterCid())) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public List<BookContentInfo> getBookCatalogList() {
        return null;
    }

    @Override
    public boolean isPageFetching() {
        return false;
    }

    /** 本地是否有缓存数据 */
    @Override
    public boolean hasLocalCachedData(String bookId, String sourceId, String chapterId, String uid) {
        Log4an.e("draw", "【本地是否有缓存】bookId=" + bookId + ",chapterId=" + chapterId + ",uid=" + uid
                + ",判断" + (DownLoadBookManager.isContentExist(bookId, sourceId, chapterId, uid)));
        return DownLoadBookManager.isContentExist(bookId, sourceId, chapterId, uid);
    }

}
